package top.aigoo.project01.controller.frontend;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import top.aigoo.project01.entity.HeadLine;
import top.aigoo.project01.entity.ShopCategory;
import top.aigoo.project01.enums.HeadLineStateEnum;
import top.aigoo.project01.enums.ShopCategoryStateEnum;
import top.aigoo.project01.service.HeadLineService;
import top.aigoo.project01.service.ShopCategoryService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 请填写该类完成的功能
 * @Author: Mr.OYSHUO.欧阳大叔  mail:1642302522@qq.com
 * @Version 1.0.0
 * @Date: 2021/9/2 0:27
 */
@Controller
@RequestMapping("/frontend")
public class MainPageController {
    @Autowired
    private ShopCategoryService shopCategoryService;

    @Autowired
    private HeadLineService headLineService;

    /**
     * 初始化前端展示系统的主页信息,包括获取一级店铺类别列表、头条列表
     *
     * @return
     */
    @RequestMapping(value = "/listmainpageinfo", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> list1stShopCategory() {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        List<ShopCategory> shopCategoryList = new ArrayList<ShopCategory>();
        try {
            //获取一级店铺类别列表(既parentId为空的shopCategory)
            shopCategoryList = shopCategoryService.getShopCategoryList(null);
            modelMap.put("shopCategoryList", shopCategoryList);
        } catch (Exception e) {
            e.printStackTrace();
            ShopCategoryStateEnum s = ShopCategoryStateEnum.INNER_ERROR;
            modelMap.put("success", false);
            modelMap.put("errMsg", s.getStateInfo());
            return modelMap;
        }

        List<HeadLine> headLineList = new ArrayList<HeadLine>();
        try {
            //获取状态为可用(1)的头条列表
            HeadLine headLineCondition = new HeadLine();
            headLineCondition.setEnableStatus(1);
            headLineList = headLineService.getHeadLineList(headLineCondition);
            modelMap.put("headLineList", headLineList);
        } catch (Exception e) {
            e.printStackTrace();
            HeadLineStateEnum s = HeadLineStateEnum.INNER_ERROR;
            modelMap.put("success", false);
            modelMap.put("errMsg", s.getStateInfo());
            return modelMap;
        }
        modelMap.put("success", true);
        return modelMap;
    }
}
